﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/25 星期六 8:23:13
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;
using Sector7.Framework.Core;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 药品库存数据访问对象
    /// </summary>
    public class DrugStockDAO : DAOBase<M7_DrugStock>, IDrugStockDAO
    {
        #region Public functions

        public List<M7_DrugStock> ReadAll(params EntityConditionBase[] conditions)
        {
            string sql = string.Format(@"
                SELECT *
                    FROM {0}", C_M7_TableName.DRUG_STOCK);
            
            var sqlParams = new List<SqlParameter>();
            for (int i = 0; i < conditions.Length; i++)
            {
                if (i == 0)
                {
                    sql += " WHERE";
                }
                var condition = conditions[i];
                sql += string.Format(" {0} = {1} {2}", condition.ConditionName, condition.ConditionValue,
                    (i < conditions.Length - 1) ? "AND" : "");
                sqlParams.Add(DBUtil.CreateSqlParameter(condition.ConditionValue, condition.ConditionName));
            }

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql, sqlParams.ToArray());

                List<M7_DrugStock> stocks = new List<M7_DrugStock>();
                if (reader.Read())
                {
                    var stock = new M7_DrugStock();
                    DBUtil.ReadData(reader, stock);

                    stocks.Add(stock);
                }
                return stocks;
            }
        }

        public M7_DrugStock Read(long drugId, long specId, int unitId)
        {
            M7_DrugStock stock = null;
            string sql = @"
                SELECT *
                FROM M7_DrugStock
                WHERE
                    DrugId = @DrugId AND
                    SpecId = @SpecId AND
                    UnitId = @UnitId";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(drugId, C_ColumnName.DRUG_ID),
                    DBUtil.CreateSqlParameter(specId, C_ColumnName.SPEC_ID),
                    DBUtil.CreateSqlParameter(unitId, C_ColumnName.UNIT_ID));
                if (reader.Read())
                {
                    stock = new M7_DrugStock();
                    DBUtil.ReadData(reader, stock);
                }
            }

            return stock;
        }

        #endregion
    }
}
